package main 

import ( "fmt" 
         "log" 
         "net/http" 
         "encoding/json"
		 "io/ioutil"
		 "os"
		 //"strconv"
		 "os/exec"
)


type UserRequest struct {
    Name  string   
    Info  string  
}


type Led_Status_Request struct {
    DeviceSN  string   
    ProductSN  string  
	Status     string
}

type UserRespon struct {
    Resualt  int    
}

var led_state = -1

func main() { 
    http.HandleFunc("/", handler) // handler 为向url发送请求时，调用的函数 
	http.HandleFunc("/LED",Led)
	http.HandleFunc("/Switch",Switch)
	http.HandleFunc("/Led_Status",Led_Status)
	http.HandleFunc("/Switch_status",Switch_status)
    log.Fatal(http.ListenAndServe(":8088",nil))
}
func Switch_status(w http.ResponseWriter, r *http.Request) { 
    
	r.ParseForm() 
	 if r.Method=="POST"{ 
		len := r.ContentLength
		body := make([]byte, len)
		r.Body.Read(body)
		b_s := string(body)		
		fmt.Println(b_s)  
			if (b_s == "LED_ON") {
				led_state = 1
			}else if (b_s == "LED_OFF") {
				led_state = 0
			}
			fmt.Fprintf(w,b_s)
		}
}


func Led(w http.ResponseWriter, r *http.Request) { 
    
	r.ParseForm() 
	//fmt.Println("led")
	//fmt.Println(r.Method)
	 if r.Method=="GET"{ 
			if (led_state == 0) {
				fmt.Fprintf(w,"LED_OFF")
			}else if (led_state == 1) {
				fmt.Fprintf(w,"LED_ON")
			}else if (led_state == -1) {
			    fmt.Fprintf(w,"LED_OFF_LINE")
			}
		}
}

func Led_Status(w http.ResponseWriter, r *http.Request) { 
    
	r.ParseForm() 
	//fmt.Println("led")
	//fmt.Println(r.Method)
	 if r.Method=="POST"{  
    	 w.Header().Set("Access-Control-Allow-Origin", "*") 
        //fmt.Println("Recived info:",r.Form) 
        len := r.ContentLength
        body := make([]byte, len)
        r.Body.Read(body)
        b_s := string(body)		
		fmt.Println(b_s)  
		var led_s_req Led_Status_Request	
		var userresp UserRespon
		err := json.Unmarshal(body, &led_s_req)
		if err != nil {
			userresp.Resualt = -1
		}else{
			fmt.Println(led_s_req.DeviceSN)
			fmt.Println(led_s_req.ProductSN)
			fmt.Println(led_s_req.Status)
			if (led_s_req.Status == "Online"){
				led_state = 0
			}else if (led_s_req.Status == "Offline"){
				led_state = -1
			}
			userresp.Resualt = 0
		}
		u_resp_j,err := json.Marshal(userresp)
		if err != nil {return}
		fmt.Println(string(u_resp_j))
		fmt.Fprintf(w,string(u_resp_j))
		}
}

func Switch(w http.ResponseWriter, r *http.Request) { 
    
	r.ParseForm() 
	fmt.Println("Switch")
	fmt.Println(r.Method)
	 if r.Method=="POST"{ 
		len := r.ContentLength
        body := make([]byte, len)
        r.Body.Read(body)
        b_s := string(body)		
		fmt.Println(b_s)  
		
		args := []string{"ucloud_s.py", b_s}
		out, err := exec.Command("python3", args...).Output()
		if err != nil {
				return
			}
		result := string(out)
		fmt.Println(result)  
			if (b_s == "LED_ON") {
				led_state = 1
			}else if (b_s == "LED_OFF") {
				led_state = 0
			}

		fmt.Fprintf(w,b_s)
		}
}

func handler(w http.ResponseWriter, r *http.Request) { 
    r.ParseForm() 
	fmt.Println("handler")
    fmt.Println(r.Method)
    if r.Method=="GET"{ 
		fin, err := os.Open("./index.html")
		if err != nil {
		    fmt.Println("index.html is null")
		}
        defer fin.Close()
		fd, _ := ioutil.ReadAll(fin)
		fmt.Fprintf(w,string(fd))
    }else{
        w.Header().Set("Access-Control-Allow-Origin", "*") 
        //fmt.Println("Recived info:",r.Form) 
        len := r.ContentLength
        body := make([]byte, len)
        r.Body.Read(body)
        b_s := string(body)		
		fmt.Println(b_s)  
		/*
		var userreq UserRequest	
		var userresp UserRespon
		err := json.Unmarshal(body, &userreq)
		if err != nil {
			userresp.Resualt = -1
		}else if (userreq.Name == "" || userreq.Info == "") {
			userresp.Resualt = -1
		}else{
			fmt.Println(userreq.Info)
			fmt.Println(userreq.Name)
			userresp.Resualt = 0
		}
		u_resp_j,err := json.Marshal(userresp)
		if err != nil {return}
		fmt.Println(string(u_resp_j))
		fmt.Fprintf(w,string(u_resp_j))
		*/
		fmt.Fprintf(w,b_s)
    }
}
